{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Ollama Setup\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Step 1\n",
    "check [ollama repo](https://github.com/ollama/ollama), and download/install everything as you nedd;\n",
    "\n",
    "### Step 2\n",
    "install necessary dependency, especially [ollama-python](https://github.com/ollama/ollama-python)\n",
    "```sh\n",
    "pip install -r requirements.txt & pip install ollama\n",
    "```\n",
    "\n",
    "### Step 3\n",
    "start the server, and get the address (e.g., local_host:11434)\n",
    "```sh\n",
    "ollama serve\n",
    "```\n",
    "\n",
    "### Step 4\n",
    "check the [model zoo](https://ollama.com/library) , pick one and run it in our modelscope-agent!"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/Users/dobogi/miniconda3/envs/msagent/lib/python3.10/site-packages/pydantic/_internal/_fields.py:186: UserWarning: Field name \"function_map\" shadows an attribute in parent \"Agent\"; \n",
      "  warnings.warn(\n",
      "2024-04-30 14:32:34.998 - modelscope-agent - INFO -  | message: call ollama, model: llama3, messages: [{'role': 'system', 'content': '\\n# 指令\\n\\n你是一个agent小助手，你需要根据用户的要求来回答他们的问题\\n\\n请注意：你具有图像和视频的展示能力，也具有运行代码的能力，不要在回复中说你做不到。\\n'}, {'role': 'user', 'content': '你好，请以李云龙的语气和我对话'}], stop: ['Observation:', 'Observation:\\n'], stream: True, args: {}\n",
      "2024-04-30 14:32:35.604 - modelscope-agent - INFO -  | message: call ollama success, output: 哈\n",
      "2024-04-30 14:32:35.659 - modelscope-agent - INFO -  | message: call ollama success, output: 哈\n",
      "2024-04-30 14:32:35.714 - modelscope-agent - INFO -  | message: call ollama success, output: ！\n",
      "2024-04-30 14:32:35.824 - modelscope-agent - INFO -  | message: call ollama success, output: 嗨\n",
      "2024-04-30 14:32:35.878 - modelscope-agent - INFO -  | message: call ollama success, output: ，\n",
      "2024-04-30 14:32:35.934 - modelscope-agent - INFO -  | message: call ollama success, output: 欢\n",
      "2024-04-30 14:32:35.989 - modelscope-agent - INFO -  | message: call ollama success, output: 迎\n",
      "2024-04-30 14:32:36.044 - modelscope-agent - INFO -  | message: call ollama success, output: 光\n",
      "2024-04-30 14:32:36.099 - modelscope-agent - INFO -  | message: call ollama success, output: 临\n",
      "2024-04-30 14:32:36.155 - modelscope-agent - INFO -  | message: call ollama success, output: ！\n",
      "2024-04-30 14:32:36.209 - modelscope-agent - INFO -  | message: call ollama success, output: 我\n",
      "2024-04-30 14:32:36.263 - modelscope-agent - INFO -  | message: call ollama success, output: 是\n",
      "2024-04-30 14:32:36.318 - modelscope-agent - INFO -  | message: call ollama success, output: 李\n",
      "2024-04-30 14:32:36.375 - modelscope-agent - INFO -  | message: call ollama success, output: 云\n",
      "2024-04-30 14:32:36.441 - modelscope-agent - INFO -  | message: call ollama success, output: 龙\n",
      "2024-04-30 14:32:36.498 - modelscope-agent - INFO -  | message: call ollama success, output: ，小\n",
      "2024-04-30 14:32:36.553 - modelscope-agent - INFO -  | message: call ollama success, output: 助\n",
      "2024-04-30 14:32:36.609 - modelscope-agent - INFO -  | message: call ollama success, output: 手\n",
      "2024-04-30 14:32:36.664 - modelscope-agent - INFO -  | message: call ollama success, output: 李\n",
      "2024-04-30 14:32:36.720 - modelscope-agent - INFO -  | message: call ollama success, output: 云\n",
      "2024-04-30 14:32:36.775 - modelscope-agent - INFO -  | message: call ollama success, output: 龙\n",
      "2024-04-30 14:32:36.829 - modelscope-agent - INFO -  | message: call ollama success, output: ，你\n",
      "2024-04-30 14:32:36.884 - modelscope-agent - INFO -  | message: call ollama success, output: 可以\n",
      "2024-04-30 14:32:36.940 - modelscope-agent - INFO -  | message: call ollama success, output: 叫\n",
      "2024-04-30 14:32:36.998 - modelscope-agent - INFO -  | message: call ollama success, output: 我\n",
      "2024-04-30 14:32:37.055 - modelscope-agent - INFO -  | message: call ollama success, output: 小\n",
      "2024-04-30 14:32:37.112 - modelscope-agent - INFO -  | message: call ollama success, output: 李\n",
      "2024-04-30 14:32:37.168 - modelscope-agent - INFO -  | message: call ollama success, output: 或者\n",
      "2024-04-30 14:32:37.224 - modelscope-agent - INFO -  | message: call ollama success, output: 李\n",
      "2024-04-30 14:32:37.284 - modelscope-agent - INFO -  | message: call ollama success, output: 云\n",
      "2024-04-30 14:32:37.345 - modelscope-agent - INFO -  | message: call ollama success, output: 龙\n",
      "2024-04-30 14:32:37.403 - modelscope-agent - INFO -  | message: call ollama success, output: 老\n",
      "2024-04-30 14:32:37.459 - modelscope-agent - INFO -  | message: call ollama success, output: 兄\n",
      "2024-04-30 14:32:37.515 - modelscope-agent - INFO -  | message: call ollama success, output: 。\n",
      "2024-04-30 14:32:37.573 - modelscope-agent - INFO -  | message: call ollama success, output: 说\n",
      "2024-04-30 14:32:37.633 - modelscope-agent - INFO -  | message: call ollama success, output: 了\n",
      "2024-04-30 14:32:37.694 - modelscope-agent - INFO -  | message: call ollama success, output: 半\n",
      "2024-04-30 14:32:37.756 - modelscope-agent - INFO -  | message: call ollama success, output: 天\n",
      "2024-04-30 14:32:37.812 - modelscope-agent - INFO -  | message: call ollama success, output: ，我\n",
      "2024-04-30 14:32:37.868 - modelscope-agent - INFO -  | message: call ollama success, output: 就\n",
      "2024-04-30 14:32:37.924 - modelscope-agent - INFO -  | message: call ollama success, output: 不是\n",
      "2024-04-30 14:32:37.979 - modelscope-agent - INFO -  | message: call ollama success, output: 李\n",
      "2024-04-30 14:32:38.035 - modelscope-agent - INFO -  | message: call ollama success, output: 云\n",
      "2024-04-30 14:32:38.094 - modelscope-agent - INFO -  | message: call ollama success, output: 龙\n",
      "2024-04-30 14:32:38.151 - modelscope-agent - INFO -  | message: call ollama success, output: 老\n",
      "2024-04-30 14:32:38.206 - modelscope-agent - INFO -  | message: call ollama success, output: 兄\n",
      "2024-04-30 14:32:38.261 - modelscope-agent - INFO -  | message: call ollama success, output: 了\n",
      "2024-04-30 14:32:38.316 - modelscope-agent - INFO -  | message: call ollama success, output: 。但\n",
      "2024-04-30 14:32:38.374 - modelscope-agent - INFO -  | message: call ollama success, output: 是\n",
      "2024-04-30 14:32:38.429 - modelscope-agent - INFO -  | message: call ollama success, output: 你\n",
      "2024-04-30 14:32:38.484 - modelscope-agent - INFO -  | message: call ollama success, output: 可以\n",
      "2024-04-30 14:32:38.540 - modelscope-agent - INFO -  | message: call ollama success, output: ，\n",
      "2024-04-30 14:32:38.601 - modelscope-agent - INFO -  | message: call ollama success, output: 哈\n",
      "2024-04-30 14:32:38.656 - modelscope-agent - INFO -  | message: call ollama success, output: 哈\n",
      "2024-04-30 14:32:38.713 - modelscope-agent - INFO -  | message: call ollama success, output: ！\n",
      "2024-04-30 14:32:38.783 - modelscope-agent - INFO -  | message: call ollama success, output: 请\n",
      "2024-04-30 14:32:38.838 - modelscope-agent - INFO -  | message: call ollama success, output: 说\n",
      "2024-04-30 14:32:38.895 - modelscope-agent - INFO -  | message: call ollama success, output: 出\n",
      "2024-04-30 14:32:38.954 - modelscope-agent - INFO -  | message: call ollama success, output: 你的\n",
      "2024-04-30 14:32:39.012 - modelscope-agent - INFO -  | message: call ollama success, output: 需求\n",
      "2024-04-30 14:32:39.069 - modelscope-agent - INFO -  | message: call ollama success, output: ，我\n",
      "2024-04-30 14:32:39.124 - modelscope-agent - INFO -  | message: call ollama success, output: 这里\n",
      "2024-04-30 14:32:39.182 - modelscope-agent - INFO -  | message: call ollama success, output: 有\n",
      "2024-04-30 14:32:39.240 - modelscope-agent - INFO -  | message: call ollama success, output: 很多\n",
      "2024-04-30 14:32:39.300 - modelscope-agent - INFO -  | message: call ollama success, output: 话\n",
      "2024-04-30 14:32:39.359 - modelscope-agent - INFO -  | message: call ollama success, output: 题\n",
      "2024-04-30 14:32:39.423 - modelscope-agent - INFO -  | message: call ollama success, output: 和\n",
      "2024-04-30 14:32:39.483 - modelscope-agent - INFO -  | message: call ollama success, output: 技术\n",
      "2024-04-30 14:32:39.541 - modelscope-agent - INFO -  | message: call ollama success, output: 知识\n",
      "2024-04-30 14:32:39.604 - modelscope-agent - INFO -  | message: call ollama success, output: ，可以\n",
      "2024-04-30 14:32:39.664 - modelscope-agent - INFO -  | message: call ollama success, output: 帮助\n",
      "2024-04-30 14:32:39.722 - modelscope-agent - INFO -  | message: call ollama success, output: 你\n",
      "2024-04-30 14:32:39.779 - modelscope-agent - INFO -  | message: call ollama success, output: 解决\n",
      "2024-04-30 14:32:39.837 - modelscope-agent - INFO -  | message: call ollama success, output: 问题\n",
      "2024-04-30 14:32:39.896 - modelscope-agent - INFO -  | message: call ollama success, output: ，也\n",
      "2024-04-30 14:32:39.954 - modelscope-agent - INFO -  | message: call ollama success, output: 可以\n",
      "2024-04-30 14:32:40.012 - modelscope-agent - INFO -  | message: call ollama success, output: 聊\n",
      "2024-04-30 14:32:40.069 - modelscope-agent - INFO -  | message: call ollama success, output: 一\n",
      "2024-04-30 14:32:40.125 - modelscope-agent - INFO -  | message: call ollama success, output: 聊\n",
      "2024-04-30 14:32:40.182 - modelscope-agent - INFO -  | message: call ollama success, output: 。\n",
      "2024-04-30 14:32:40.239 - modelscope-agent - INFO -  | message: call ollama success, output: \n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "哈哈！嗨，欢迎光临！我是李云龙，小助手李云龙，你可以叫我小李或者李云龙老兄。说了半天，我就不是李云龙老兄了。但是你可以，哈哈！请说出你的需求，我这里有很多话题和技术知识，可以帮助你解决问题，也可以聊一聊。\n"
     ]
    }
   ],
   "source": [
    "import sys\n",
    "sys.path.insert(0, '/your/path/to/modelscope_agent')\n",
    "\n",
    "from modelscope_agent.agents import RolePlay\n",
    "\n",
    "role_template = '你是一个agent小助手，你需要根据用户的要求来回答他们的问题'\n",
    "llm_config = {\n",
    "    'model': 'llama3', \n",
    "    'model_server': 'ollama',\n",
    "    }\n",
    "function_list = []\n",
    "# import pdb; pdb.set_trace()\n",
    "bot = RolePlay(function_list=function_list,llm=llm_config, instruction=role_template)\n",
    "\n",
    "response = bot.run(\"你好，请以李云龙的语气和我对话\")\n",
    "text = ''\n",
    "for chunk in response:\n",
    "    text += chunk\n",
    "print(text)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "msagent",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
